package cn.jr.dao.impl;

import cn.jr.dao.IOrderDao;
import cn.jr.pojo.Order;
import cn.jr.util.DBHelper;


import java.util.ArrayList;

@SuppressWarnings("all")
public class OrderDaoImpl implements IOrderDao {

    DBHelper db = new DBHelper();

    @Override//创建订单表
    public int insertOrder(Order order) {
        String sql = "insert into torder values(null,?,'0.00','0.00',?)";
        Object[] objs = {order.getOrderNumber(), order.getOrderTime()};
        return db.dtUpdate(sql, objs);
    }

    @Override//查询 某 订单表 通过 订单主键
    public Order queryOrderById(int orderId) {
        String sql = "select * from torder where orderId = ?";
        ArrayList<Order> list = db.dtQuery(sql, Order.class,orderId);
        return list.size() > 0 ? list.get(0) : new Order();
    }

    //查询 一条订单 by 订单编号
    @Override
    public Order queryOrderByOrderNumber(String orderNumber) {
        String sql = "select * from torder where orderNumber = ?";
        ArrayList<Order> list = db.dtQuery(sql, Order.class,orderNumber);
        return list.size() > 0 ? list.get(0) : new Order();
    }

    //通过 订单编号 查询 订单表主键
    @Override
    public int queryOrderIdByOrderNumber(String orderNumber) {
        String sql = "select * from torder where orderNumber = ?";
        ArrayList<Order> list = db.dtQuery(sql, Order.class,orderNumber);
        return list.size()>0?list.get(0).getOrderId():0;//0就是没有对应订单
    }

    @Override//更新数据
    public int updateOrder(double waresAllPrice,double orderReallyPrice,int orderId) {
        String sql = "update torder set waresAllPrice = ?, orderReallyPrice = ? where orderId = ?";
        Object[] objs = {waresAllPrice, orderReallyPrice, orderId};
        return db.dtUpdate(sql,objs);
    }

    @Override//查询全部
    public ArrayList<Order> queryAllOrderProject() {
        String sql = "select * from torder";
        return db.dtQuery(sql, Order.class);
    }
}
